<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */
namespace app\common\lib;

use app\common\controller\Base;

class Sms extends Base
{
    //短信发送途径，设置大于0的正整数
    const SMS_REGISTER = 1;  //注册
    const SMS_EDIT_PHONE = 2;  //修改手机号
    const SMS_FIND_BACK_PASSWORD = 3; //找回密码
    const SMS_BIND_PHONE = 4; //绑定手机号

    //private $accessKeyId = ''; //身份授权key
    //private $accessKeySecret = '';//实时快递查询接口的公司编号
    //private $templateCode = '';

    /**
     * 发送短信验证码
     * @param $phoneNumber
     * @param int $type 途径
     * @param string $templateCode SMS_150180413为验证码code
     * @return array|bool
     */
    public function sendSmsVerifyCode($phoneNumber = '',$type = '',$templateCode = ''){
        if (empty($phoneNumber) || empty($type)) {
            return array(
                'status' => false,
                'msg' => '手机号或短信用途不能为空！'
            );
        }
        //默认templateCode 为短信验证码
        $templateCode = empty($templateCode) ? config(GlobalConfig::ALIYUN_SMS)['verify_template_code'] : $templateCode;
        $code = rand(100000,999999);
        $param = array('code' => $code);
        $result = $this->sendSms($phoneNumber,$templateCode,$param);
        if($result->Code == 'OK'){
            //保存数据库
            $saveData = array(
                'phone' => $phoneNumber,
                'type' => $type,
                'tmp_code' => $templateCode,
                'code' => $code,
                'content' => "您的手机验证码：" . $code .', 5min后过期，请勿泄露。如非本人操作，请忽略此短信。谢谢！',
                'create_time' => time()
            );
            if(db('sms_log')->insertGetId($saveData)){
                return array(
                    'status' => true,
                    'msg' => '发送成功！'
                );
            }else{
                return array(
                    'status' => false,
                    'msg' => '发送失败！'
                );
            }
        }else{
            return array(
                'status' => false,
                'msg' => '发送失败，请重新尝试！'
            );
        }
    }

    /**
     * 验证短信验证码
     * @param string $phone
     * @param string $code 验证码
     * @param int $type 途径
     * @return bool
     */
    public function checkVerifyCode($phone = '',$code = '',$type = ''){
        if(empty($phone) || empty($code) || empty($type)){
            return false;
        }
        $map = array(
            'phone' => $phone,
            'code' => $code,
            'type' => $type
        );
        $sms_log = db('sms_log')->where($map)->order('create_time desc')->find();
        if($sms_log){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 发送单条短信
     * @return stdClass
     */
    /**
     * 发送单条短信
     * @param $phoneNumers 手机号
     * @param $templateCode  模板编号
     * @param array $param  参数
     * @param string $signName 签名名称
     * @return mixed|\SimpleXMLElement
     */
    private function sendSms($phoneNumers,$templateCode,$param = array(),$signName = '') {
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/aliyunSms/Sms.php';
        $accessKeyId = config(GlobalConfig::ALIYUN_SMS)['access_key_id'];
        $accessKeySecret = config(GlobalConfig::ALIYUN_SMS)['access_key_secret'];
        $signName = empty($signName) ? config(GlobalConfig::ALIYUN_SMS)['sign_name'] : $signName;
        $sms = new \Sms($accessKeyId, $accessKeySecret);
        return $sms->sendSms($phoneNumers,$templateCode,$param,$signName);
    }

}